﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;

public class ObjectInteractionScreen : GameScreen
{
    private SpriteFont font;
    private Texture2D textSpace, dark;
    private Texture2D fundo;
    private Texture2D sair;
    private List<Objects> objectList;
    private bool clicked;

    private Vector2 exitPosition;

    private bool isMouseOverExit;

    private int interactionCount = 0;
    private Interaction interaction;

    int objSelected = 0;

    public ObjectInteractionScreen(Interaction interaction)
    {
        IsPopup = true;
        Globals.IsPaused = true;

        this.interaction = interaction;
        
        objectList = interaction.ObjList;

        CheckAndRemoveObjects();

        interactionCount = GameSession.GetInteractionCount() - interaction.InteractionCounter;

        if (interactionCount > objectList.Count)
            interactionCount = objectList.Count;

        if (interactionCount < 0)
            interactionCount = 0;

        GameSession.ScreenManager.GameRef.IsMouseVisible = true;
    }

    private void CheckAndRemoveObjects()
    {
        for (int i = objectList.Count - 1; i >= 0; i--)
        {
            if (!GameSession.CheckAllConditions(objectList[i].Condition))
            {
                objectList.RemoveAt(i);
            }
        }
    }

    private void CheckAndRemoveObjects(Objects toRemove)
    {
        for (int i = objectList.Count - 1; i >= 0; i--)
        {
            if (objectList[i].IsSameObject(toRemove))
                continue;

            if (!GameSession.CheckAllConditions(objectList[i].Condition))
            {
                objectList.RemoveAt(i);
            }
        }
    }

    public override void LoadContent()
    {
        base.LoadContent();

        dark = GameSession.Content.Load<Texture2D>("Textures/dark");
        textSpace = GameSession.Content.Load<Texture2D>("Textures/paper");
        fundo = GameSession.Content.Load<Texture2D>("Objetos/mesa");
        sair = GameSession.Content.Load<Texture2D>("Objetos/fechar");

        exitPosition = new Vector2(Globals.ScreenWidth - sair.Width - 30, 30);

        font = GameSession.Content.Load<SpriteFont>("SpriteFont1");
    }

    int aux = 0;

    Objects selected = null, toRemove = null;

    Vector2 mouse;
    MoveSelect moving = MoveSelect.KEYBOARD;

    public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
    {

        if (InputManager.IsActionTriggered(InputManager.Action.CursorDown)
            || InputManager.IsActionTriggered(InputManager.Action.CursorLeft))
        {
            objSelected++;
            if (objSelected > (int)objectList.Count - 1)
                objSelected = 0;
            moving = MoveSelect.KEYBOARD;
        }
        if (InputManager.IsActionTriggered(InputManager.Action.CursorUp)
            || InputManager.IsActionTriggered(InputManager.Action.CursorRight))
        {
            objSelected--;
            if (objSelected < 0)
                objSelected = (int)objectList.Count - 1;
            moving = MoveSelect.KEYBOARD;
        }


        MouseState state = Mouse.GetState();

        if (!(mouse.X + 5 > state.X - 20 && mouse.X - 5 < state.X - 20) || !(mouse.Y + 5 > state.Y && mouse.Y - 5 < state.Y))
        {
            mouse.X = state.X - 20;
            mouse.Y = state.Y;
            moving = MoveSelect.MOUSE;
        }

        base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

        if (state.X > exitPosition.X && state.Y > exitPosition.Y && state.X < exitPosition.X + sair.Width && state.Y < exitPosition.Y + sair.Height)
        {
            isMouseOverExit = true;
        }
        else
        {
            isMouseOverExit = false;
        }

        if (isMouseOverExit && state.LeftButton == ButtonState.Pressed)
            Exit();

        bool remove = false;

        int i = 0;

        foreach (Objects obj in objectList)
        {
            int oldcount = interactionCount;
            obj.Update(objectList, ref interactionCount, moving, ref objSelected, i);
            if (oldcount != interactionCount)
            {
                interaction.InteractionCounter++;
                clicked = true;
                aux = 0;
            }
            i++;
        }

        foreach (Objects obj2 in objectList)
        {
            if (obj2.Clicked && selected != obj2)
            {
                remove = true;
                toRemove = selected;
                selected = obj2;
            }
            if (selected == null && obj2.Clicked)
                selected = obj2;
        }

        if(remove)
        {
            CheckAndRemoveObjects(selected);
            remove = false;
        }

        if (aux > 20)
            clicked = false;
        else
            aux++;


        if (Globals.FirstItem)
        {
            ScreenManager.AddScreen(new TutorialScreen(TutorialScreen.TutorialKind.ITEM));
            Globals.FirstItem = false;
        }
    }

    public override void Draw(GameTime gameTime)
    {
        base.Draw(gameTime);

        SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

        spriteBatch.Begin();

        spriteBatch.Draw(dark, Vector2.Zero, Color.White);

        if(interaction.Fundo != null)
            spriteBatch.Draw(interaction.Fundo, new Rectangle(Globals.ScreenWidth / 2 - interaction.Fundo.Width / 2, 600 / 2 - interaction.Fundo.Height / 2, interaction.Fundo.Width, interaction.Fundo.Height), Color.White);
        
        if(interaction.ShowTextSpace)
            spriteBatch.Draw(textSpace, new Rectangle(Globals.ScreenWidth/2 - textSpace.Width/2, 600, textSpace.Width, textSpace.Height), Color.White);

        foreach(Objects obj in objectList)
        {
            obj.Draw(spriteBatch);
            //spriteBatch.Draw(obj.Imagem, new Rectangle((int)obj.Posicao.X, (int)obj.Posicao.Y, obj.Imagem.Width, obj.Imagem.Height), Color.White);
        }

        if (isMouseOverExit)
            spriteBatch.Draw(sair, exitPosition, Color.Red);
        else
            spriteBatch.Draw(sair, exitPosition, Color.White);

        if (interaction.ShowTextSpace)
        {
            string str = "Você pode interagir com " + interactionCount + " objeto(s).";
            if (clicked)
                spriteBatch.DrawString(font, str, new Vector2(Globals.ScreenWidth / 2 - font.MeasureString(str).X / 2, 570), Color.Yellow);
            else
                spriteBatch.DrawString(font, str, new Vector2(Globals.ScreenWidth / 2 - font.MeasureString(str).X / 2, 570), Color.White);
        }

        spriteBatch.End();
    }

    public override void HandleInput()
    {
        base.HandleInput();

        if (InputManager.IsActionTriggered(InputManager.Action.Back))
        {
            GameSession.ScreenManager.GameRef.IsMouseVisible = false;
            Exit();
        }
    }

    private void Exit()
    {
        foreach (Objects obj in objectList)
            obj.Deselect();

        Globals.IsPaused = false;
        GameSession.ScreenManager.GameRef.IsMouseVisible = false;
        ExitScreen();
    }

    public override void UnloadContent()
    {
        base.UnloadContent();
    }
}
